<?php 
class ControllerMessageAdvice extends Controller {
	public function index() {  
		$this->language->load('message/advice');

		$this->load->model('message/advice');

		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		
			$this->document->setTitle("意见和建议");
			
			if(isset($this->request->get['sender_name'])){
				$sender_name=$this->request->get['sender_name'];
			}
			else{
				$sender_name = '';
			}
			$this->data['insert'] = $this->url->link('message/advice/insert','sender_name='.$sender_name,'SSL');
			
			$limit = $this->config->get('config_catalog_limit');
			$data = array(
				'start'             => ($page - 1) * $limit,
				'limit'             => $limit
			);
			
			$results = $this->model_message_advice->getAdvices($data);
			$this->data['advices'] = array();
			if($results){
				$page = ($page+1);
				foreach ($results as $result) {
					
					$contents = array();
					$contentss = $this->model_message_advice->getAdviceReplies($result['advice_id']);
					if($contentss){
						$contents = $contentss;
					}
					$this->data['advices'][] = array(
						'title'=>$result['title'],
						'advice'=>$result['advice'],
						'contents'=>$contents
					);
				}
			}
			
			$this->data['more'] = $this->url->link('message/advice','page='.$page,'SSL');
			$this->data['before'] = $this->url->link('message/advice','page='.($page-1),'SSL');
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/message/advice.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/message/advice.tpl';
			} else {
				$this->template = 'default/template/message/advice.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);

			$this->response->setOutput($this->render());
	}

	public function insert() {
		$this->language->load('message/advice');

		$this->load->model('message/advice');
		if(isset($this->request->get['sender_name'])){
				$sender_name=$this->request->get['sender_name'];
			}
			else{
				$sender_name = '';
			}
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_message_advice->addAdvice($sender_name,$this->request->post);
			$this->data['success'] = "提交意见成功";
		}
		$this->index();
	}
}
?>